package com.chuangke.common.express;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestCkEvaluator {

	public static void main(String[] args) {
		test2() ;
//		test5();
//		test2();
//		test3();
//		test4();
		
//		Map<String, Object> env = null;
//
//		env = new HashMap<>();
////		env.put("WLSQ", "WLSQ");
////		env.put("QD", "QD");
//		env.put("cdate", new Date());
//		Object obj = CkEvaluatorExecutor.execute("'WLSQ'+date.day(cdate)+1001+'QD'+date.day()", env);
//		System.out.println(obj);
	}
	
	public static void test6() {
		Map<String, Object> env = new HashMap<>();
		env.put("a", new BigDecimal("414.00"));
		env.put("b", "22");
		Object obj = CkEvaluatorExecutor.execute("a+b", env) ;
		
		System.out.println("obj：" + obj);
	}
	
	public static void t() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			test3();
		}
		long end = System.currentTimeMillis();
		System.out.println("时间：" + (end - start));

		start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			test3();
		}
		end = System.currentTimeMillis();
		System.out.println("时间：" + (end - start));
		start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			test3();
		}
		end = System.currentTimeMillis();
		System.out.println("时间：" + (end - start));
		
		start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			test3();
		}
		end = System.currentTimeMillis();
		System.out.println("时间：" + (end - start));
	}
	
	public static void test4() {
		Map<String, Object> env = new HashMap<>();
		env.put("a", new BigDecimal("414.00"));
		env.put("b", "zsa");
		Object obj = CkEvaluatorExecutor.execute("a>57 || b=='zs'", env) ;
		
		System.out.println("obj：" + obj);
	}
	
	public static void test5() {
		Map<String, Object> env = new HashMap<>();
		env.put("a", new BigDecimal("414.00"));
//		env.put("b", new BigDecimal("0"));
		Object obj = CkEvaluatorExecutor.execute("a+b", env) ;
		System.out.println("obj：" + obj);
		
//		String f = "Sheet1_E2+Sheet1_E3*Sheet1_E5" ;
//		
//		List<String> list = CkEvaluatorExecutor.getVariableNames(f) ;
//		list.forEach(s -> System.out.println(s));
		
		
//		System.out.println(CkEvaluatorExecutor.execute("string.indexOf()", env));
		
		
	}

	/**
	 * 测试复合公式计算
	 */
	public static void test3() {
//		Map<String, Object> env = new HashMap<String, Object>();
//		env.put("a", new BigDecimal("44.00"));
//		env.put("b", new BigDecimal("244.00"));
//		String formula = "a+b";
//		CkEvaluatorExecutor.execute(formula, env,true);
	}

	/**
	 * 测试复合公式计算
	 */
	public static void test1() {
		List<CkExpression> list = new ArrayList<>();
//		list.add(new CkExpression("D2", "B2+3.00"));
//		list.add(new CkExpression("A2", "F3+5.00"));
//		list.add(new CkExpression("B2", "A2+7.00"));
//		list.add(new CkExpression("C2", "F3+7.00"));
//		list.add(new CkExpression("B3", "N7+4.00"));
//		list.add(new CkExpression("N7", "E7+4.00"));
//		list.add(new CkExpression("E2", "N7+3.00"));
//		list.add(new CkExpression("N2", "A2+3.00"));

		// 循环引用测试
		list.add(new CkExpression("A2", "B2+3.00"));
		list.add(new CkExpression("B2", "N2+3.00"));
		list.add(new CkExpression("N2", "A2+3.00"));

		Collection<CkExpression> result = CkEvaluatorExecutor.parse(list);
		System.out.println(result);
//
		Map<String, Object> env = new HashMap<>();
		env.put("F3", new BigDecimal("44.00"));
		env.put("E7", new BigDecimal("244.00"));

		env.put("F3", 44.00);
		env.put("E7", 244.00);
		Map<String, Object> obj = CkEvaluatorExecutor.execute(list, env);
		System.out.println(obj);
	}

	/**
	 * 测试普通自定义公式计算
	 *
     */
	public static void test2() {
		List<String> varNames = CkEvaluator.getVariableNames("date.year(period,data_formate)+43+tax");
		for (String f : varNames) {
			System.out.println(f);
		}

		Map<String, Object> env = null;

		env = new HashMap<>();
		env.put("cdate", new Date());
		Object obj = CkEvaluatorExecutor.execute("date.year(cdate)", env);
		System.out.println(obj);

		obj = CkEvaluatorExecutor.execute("date.year('201901','yyyyMM')", null);
		System.out.println(obj);

		env = new HashMap<>();
		env.put("period", "201901");
		env.put("data_formate", "yyyyMM");
		obj = CkEvaluatorExecutor.execute("date.year(period,data_formate)", env);
		System.out.println(obj);

		env = new HashMap<>();
		env.put("cdate", new Date());
		obj = CkEvaluatorExecutor.execute("date.quarter(cdate)", env);
		System.out.println(obj);

		obj = CkEvaluatorExecutor.execute("date.quarter('201901','yyyyMM')", null);
		System.out.println(obj);
	}
}
